package generator.website;

import java.util.Iterator;
import org.eclipse.emf.common.util.EList;
import website.*;

public class WebSiteServletActionFactory
{
  protected static String nl;
  public static synchronized WebSiteServletActionFactory create(String lineSeparator)
  {
    nl = lineSeparator;
    WebSiteServletActionFactory result = new WebSiteServletActionFactory();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "package servlets;" + NL + "" + NL + "import javax.servlet.http.HttpServletRequest;" + NL + "import javax.servlet.http.HttpServletResponse;" + NL + "import org.apache.struts.Globals;" + NL + "import org.apache.struts.action.Action;" + NL + "import org.apache.struts.action.ActionForm;" + NL + "import org.apache.struts.action.ActionForward;" + NL + "import org.apache.struts.action.ActionMapping;" + NL + "import org.apache.struts.action.ActionMessage;" + NL + "import org.apache.struts.action.ActionMessages;" + NL + "import actionForms.";
  protected final String TEXT_2 = "ValidationForm;" + NL + "import domain.DAO";
  protected final String TEXT_3 = ";" + NL + "" + NL + "public class ";
  protected final String TEXT_4 = "Action extends Action {" + NL + "\t" + NL + "\tpublic ActionForward execute(final ActionMapping pMapping," + NL + "\t\t\tActionForm pForm, final HttpServletRequest pRequest," + NL + "\t\t\tfinal HttpServletResponse pResponse) {" + NL + "\t\t\t" + NL + "\t\tfinal ";
  protected final String TEXT_5 = "ValidationForm lForm=(";
  protected final String TEXT_6 = "ValidationForm)pForm;" + NL + "\t\t";
  protected final String TEXT_7 = NL + "\t\tfinal String [] ";
  protected final String TEXT_8 = " = lForm.get";
  protected final String TEXT_9 = "();";
  protected final String TEXT_10 = NL + "  \t\tfinal String ";
  protected final String TEXT_11 = " = lForm.get";
  protected final String TEXT_12 = "();";
  protected final String TEXT_13 = NL + "  \t\t" + NL + "  \t\tfinal DAO";
  protected final String TEXT_14 = " lDAO";
  protected final String TEXT_15 = " = new DAO";
  protected final String TEXT_16 = "();" + NL + "  \t\tfinal String lError = lDAO";
  protected final String TEXT_17 = ".";
  protected final String TEXT_18 = "(";
  protected final String TEXT_19 = ", ";
  protected final String TEXT_20 = ");";
  protected final String TEXT_21 = NL + "  \t\t" + NL + "\t\tif(lError == null) {" + NL + "\t\t\t// if no exception is raised,  forward \"success\"" + NL + "\t\t\treturn pMapping.findForward(\"success\");" + NL + "\t\t} else {" + NL + "\t\t\t// If any exception, return the \"error\" forward" + NL + "\t\t\treturn pMapping.findForward(\"error\");" + NL + "\t\t}" + NL + "\t}" + NL + "}";

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    Form form = (Form) argument;
    stringBuffer.append(TEXT_1);
    stringBuffer.append(form.getType().getName());
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_2);
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_3);
    stringBuffer.append(form.getType().getName());
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_4);
    stringBuffer.append(form.getType().getName());
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_5);
    stringBuffer.append(form.getType().getName());
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_6);
    
		EList<Tag> Listtag = form.getTags();
		for(Tag tag : Listtag){
			if(tag instanceof FormTag && !(tag instanceof Button)){
				if(tag instanceof Select || tag instanceof RadioButton){
    stringBuffer.append(TEXT_7);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_8);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_9);
    
				} else {
    stringBuffer.append(TEXT_10);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_11);
    stringBuffer.append(tag.getName());
    stringBuffer.append(TEXT_12);
    
  				}
  			}
  		}
    stringBuffer.append(TEXT_13);
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_14);
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_15);
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_16);
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_17);
    stringBuffer.append(form.getType().getName());
    stringBuffer.append(form.getEntity().getName());
    stringBuffer.append(TEXT_18);
    
  		
  		for(int i=0; i<Listtag.size(); i++){
			if(Listtag.get(i) instanceof FormTag && !(Listtag.get(i) instanceof Button)){
				if(Listtag.get(i) instanceof Select || Listtag.get(i) instanceof RadioButton){
  	  
    stringBuffer.append(Listtag.get(i).getName());
    
				} else {
  	  
    stringBuffer.append(Listtag.get(i).getName());
    
  				}
  				if(i < Listtag.size()-3){
  				
    stringBuffer.append(TEXT_19);
    
  				}
  			}
			if(i == Listtag.size()-1)
				
    stringBuffer.append(TEXT_20);
    
  		}
    stringBuffer.append(TEXT_21);
    return stringBuffer.toString();
  }
}
